package it.polito.connectedcar.trafficlight;

import it.polito.appeal.traci.ControlledLink;
import it.polito.appeal.traci.Program;
import it.polito.appeal.traci.connectedcar.sumo.SUMOTrafficLight;
import it.polito.appeal.traci.connectedcar.trafficlight.TrafficLightDataListener;

import java.util.List;
import java.util.Map;

/**
 * The adapter class is the bridge between a proxy ( known by high levels ) and
 * the source entity managing data The {@link SumoTrafficLightAdapter} is
 * attached to the simulation provided Traffic Light's representation. It
 * implements the {@link TrafficLightDataListener} interface in order to be
 * informed when the SUMO entity has fresh data to be published. The job of this
 * class is to receive updates from low levels and to forward them by using the
 * {@link TrafficLightUpdateDelegate} Delegate. By decoupling the interface
 * definitions of low level objects and high levels objects, it will be possible
 * to attach to different Source entities ( real or simulated) without changing
 * the business logic and behavior.
 * 
 * @author Nicola Aresta
 * 
 */
public class SumoTrafficLightAdapter extends SourceTrafficLightProxy implements	TrafficLightDataListener {

	private SUMOTrafficLight trafficLight;

	public SumoTrafficLightAdapter(SUMOTrafficLight trafficLight) {
		this.trafficLight = trafficLight;
		this.trafficLight.setDataListener(this);
	}

	@Override
	public void onCurrentPhaseDurationChange(int duration) {
		getDelegate().onCurrentPhaseDurationChange(duration);
	}

	public void onNextPhaseRemainingTimeChange(int remainingTime) {
		getDelegate().onNextPhaseRemainingTimeChange(remainingTime);
	};

	@Override
	public void onPhaseChanged(int currentPhase) {
		getDelegate().onPhaseChanged(currentPhase);
	}

	@Override
	public void onRYGStateChange(String RYGstate) {
		getDelegate().onRYGStateChange(RYGstate);
	}

	public void onSwitch() {

	}

	@Override
	public int getCurrentPhase() {
		return trafficLight.getCurrentPhase();
	}

	@Override
	public long getAssumedTimeToNextSwitch() {
		return trafficLight.getNextSwitchTime();
	}

	@Override
	public List<String> getControlledLanes() {
		return trafficLight.getControlledLanes();
	}

	@Override
	public List<String> getControlledLinks() {
		return null;
	}

	@Override
	public String getId() {
		return trafficLight.getID();
	}

	@Override
	public String getTlProgram() {
		// TODO Auto-generated method stub
		return trafficLight.getTlProgram();
	}

	@Override
	public void onControlledLinksChange(Map<Integer, List<ControlledLink>> controlledLinks) {
		getDelegate().onControlledLinksChange(controlledLinks);
	}

	@Override
	public void onCurrentProgramChanged(Program tlProgram) {
		getDelegate().onCurrentProgramChanged(new ConnectedTLProgram(tlProgram));
	};
}
